package com.gupaoedu.vip.pattern.factory.simplefactory;


import com.gupaoedu.vip.pattern.factory.ICourse;
import com.gupaoedu.vip.pattern.factory.JavaCourse;
import com.gupaoedu.vip.pattern.factory.PythonCourse;

/**
 * Created by Tom.
 */
public class CourseFactory {

    public ICourse create(String name){
        if("java".equals(name)){
            return new JavaCourse();
        }else if("python".equals(name)){
            return new PythonCourse();
        }else {
            return null;
        }
    }

    /**
     * 通过全路径映射
     * @param className
     * @return
     */
    public ICourse createByClassName(String className){
        try {
            if (!(null == className || "".equals(className))) {
                return (ICourse) Class.forName(className).newInstance();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

//    public ICourse create(Class<? extends ICourse> clazz){
//        try {
//            if (null != clazz) {
//                return clazz.newInstance();
//            }
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return null;
//    }
    public  ICourse createByClassObject(Class<? extends ICourse> clazz ){

        try {
            if(null != clazz) return clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return  null;
    }

}
